package com.mou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mou.constant.MessageConstant;
import com.mou.constant.TransactionConstant;
import com.mou.dto.MyOrderDTO;
import com.mou.dto.OtherOrderDTO;
import com.mou.dto.TransactionOrderDTO;
import com.mou.entity.Product;
import com.mou.entity.Transaction;
import com.mou.entity.User;
import com.mou.exception.TransactionOrderException;
import com.mou.mapper.ProductMapper;
import com.mou.mapper.TransactionMapper;
import com.mou.mapper.UserMapper;
import com.mou.service.TransactionService;
import com.mou.vo.MyOrderVO;
import com.mou.vo.TransactionVO;
import org.apache.ibatis.transaction.TransactionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("transactionServiceImpl")
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TransactionMapper transactionMapper;

    @Override
    public TransactionVO getOrderDetail(TransactionOrderDTO transactionOrderDTO) {
        TransactionVO transactionVO = transactionMapper.getId(transactionOrderDTO);
        Product product = productMapper.selectById(transactionOrderDTO.getProductId());
        User buyer = userMapper.selectById(transactionOrderDTO.getBuyerId());

        if(product==null){
            throw new TransactionException(MessageConstant.PRODUCT_NOT_FOUND);
        }
        if(buyer==null){
            throw new TransactionException(MessageConstant.ACCOUNT_NOT_EXIST);
        }
        transactionVO.setTitle(product.getTitle());
        transactionVO.setDescription(product.getDescription());
        transactionVO.setPrice(product.getPrice());
        transactionVO.setAddress("无");
        transactionVO.setNickname(buyer.getNickname());
        transactionVO.setPhone(buyer.getPhone());
        transactionVO.setImageUrl(product.getImageUrl());
        return transactionVO;
    }

    @Override
    public void add(Transaction transaction) {
        if(transactionMapper.orderCount(transaction)>0){
            throw new TransactionOrderException(MessageConstant.ORDER_EXIST_PAY);
        }
        transaction.setQuantity(1);
        transaction.setStatus(1);
        transaction.setTransactionDate(LocalDateTime.now());
        transactionMapper.insert(transaction);
    }

    @Override
    public List<MyOrderVO> getMyOrder(MyOrderDTO myOrderDTO) {
        List<Transaction> transactionList = transactionMapper.getListByBuyerId(myOrderDTO);
        List<MyOrderVO> myOrderVOS = new ArrayList<>();
        if(transactionList!=null){
            for(Transaction transaction : transactionList){
                MyOrderVO myOrderVO = new MyOrderVO();
                BeanUtils.copyProperties(transaction,myOrderVO);
                Product product = productMapper.selectById(transaction.getProductId());
                if(product!=null){
                    myOrderVO.setImageUrl(product.getImageUrl());
                    myOrderVO.setTitle(product.getTitle());
                    myOrderVO.setPrice(product.getPrice());
                    myOrderVO.setTotalPrice(product.getPrice()*transaction.getQuantity());

                }
                User buyer = userMapper.selectById(transaction.getBuyerId());
                User seller = userMapper.selectById(transaction.getSellerId());
                if(buyer!=null){
                    myOrderVO.setBuyerName(buyer.getNickname());
                    myOrderVO.setAddress("空");
                }
                if(seller!=null){
                    myOrderVO.setSellerName(seller.getNickname());
                }
                myOrderVOS.add(myOrderVO);

            }
        }
        return myOrderVOS;
    }

    @Override
    @Transactional
    public void cancel(Integer id) {
        Transaction transaction = transactionMapper.selectById(id);
        Product product = productMapper.selectById(transaction.getProductId());
        User user = userMapper.selectById(transaction.getBuyerId());
        Double amount = transaction.getQuantity()*product.getPrice();
        user.setBalance(user.getBalance()+amount);
        userMapper.updateById(user);

        User seller = userMapper.selectById(transaction.getSellerId());
        seller.setBalance(seller.getBalance()-amount);
        userMapper.updateById(seller);

        transactionMapper.cancel(id);
    }

    @Override
    public void accept(Integer id) {
        transactionMapper.accept(id);
    }

    @Override
    public void startDelivery(Integer id) {
        transactionMapper.startDelivery(id);
    }

    @Override
    public void confirmDelivery(Integer id) {
        transactionMapper.confirmDelivery(id);
    }

    @Override
    public List<MyOrderVO> getOtherOrder(OtherOrderDTO otherOrderDTO) {
        List<Transaction> transactionList = transactionMapper.getListBySellerId(otherOrderDTO);
        List<MyOrderVO> myOrderVOS = new ArrayList<>();
        if(transactionList!=null){
            for(Transaction transaction : transactionList){
                MyOrderVO myOrderVO = new MyOrderVO();
                BeanUtils.copyProperties(transaction,myOrderVO);
                Product product = productMapper.selectById(transaction.getProductId());
                if(product!=null){
                    myOrderVO.setImageUrl(product.getImageUrl());
                    myOrderVO.setTitle(product.getTitle());
                    myOrderVO.setPrice(product.getPrice());
                    myOrderVO.setTotalPrice(product.getPrice()*transaction.getQuantity());

                }
                User buyer = userMapper.selectById(transaction.getBuyerId());
                User seller = userMapper.selectById(transaction.getSellerId());
                if(buyer!=null){
                    myOrderVO.setBuyerName(buyer.getNickname());
                    myOrderVO.setAddress("空");
                }
                if(seller!=null){
                    myOrderVO.setSellerName(seller.getNickname());
                }
                myOrderVOS.add(myOrderVO);

            }
        }
        return myOrderVOS;
    }
}
